package com.example.auv_660.moduleload;

import com.example.auv_660.MainActivity;
import com.example.auv_660.network.tcp.ModuleReadHandler;
import com.example.auv_660.network.tcp.ModuleWriteHandler;
import com.example.auv_660.network.udp.HFUDPBuild;
import com.example.auv_660.util.Constants;
import com.example.auv_660.util.MsgType;
import com.example.auv_660.util.ThreadPool;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;

public class PropellerLoad extends ModuleLoad {
    private ModuleWriteHandler propellerWriteHandler;
    private ModuleReadHandler propellerReadHandler;
    private AsynchronousSocketChannel asynchronousSocketChannel;
    private MainActivity mainActivity = MainActivity.getInstance();

    private int threadKey = 0;

    public PropellerLoad() throws IOException
    {

    }

    @Override
    public void doModuleLoad(String remoteModuleIP, AsynchronousSocketChannel asynchronousSocketChannel) {
        if (remoteModuleIP.equals(Constants.PROPELLER_IP))
        {
            if (this.asynchronousSocketChannel != null)
            {
                releaseModuleResource();
            }

            this.asynchronousSocketChannel = asynchronousSocketChannel;

            if (propellerWriteHandler == null)
            {
                propellerWriteHandler = new ModuleWriteHandler(asynchronousSocketChannel);
            }

            if (propellerReadHandler == null)
            {
                propellerReadHandler = new ModuleReadHandler(asynchronousSocketChannel)
                {
                    @Override
                    public void completed(Integer result, ByteBuffer attachment)
                    {
                        if (result > 0)
                        {
                            if (Constants.ORDER)
                            {
                                HFUDPBuild.INSTANCE.getHFUdpClient().sendMessage("CCHECK:推进器");
                            }

                            Constants.PROPELLER = true;
                            attachment.flip();
                            byte[] bytes = new byte[attachment.remaining()];
                            attachment.get(bytes);
                            String propeller = null;
                            try
                            {
                                propeller = new String(bytes, "UTF-8");
                            }
                            catch (UnsupportedEncodingException e)
                            {
                                e.printStackTrace();
                            }

                            if (mainActivity != null)
                            {
                                mainActivity.sendMsgToMainThread(MsgType.PROPELLER_MSG, "命令已执行");
                            }

                            getMessageFromModule();
                        }
                    }
                };
                propellerReadHandler.getMessageFromModule();
            }

            try
            {
                if (mainActivity != null && asynchronousSocketChannel != null)
                {
                    mainActivity.sendMsgToMainThread(MsgType.PROPELLER_CONN,
                            asynchronousSocketChannel.getRemoteAddress().toString().split("/")[1]);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            if (threadKey != 0)
            {
                ThreadPool.INSTANCE.getThreadPoolProxy().shutDownScheduledTask(threadKey);
            }

            return;
        }
        else
        {
            ModuleLoad nextModule = this.getNextModule();
            if (nextModule != null)
            {
                nextModule.doModuleLoad(remoteModuleIP, asynchronousSocketChannel);
            }
        }
    }

    @Override
    public ModuleWriteHandler getWriterHandler() {
        if (propellerWriteHandler == null)
        {
            if (mainActivity != null)
            {
//                mainActivity.sendMsgToMainThread(MsgType.TOAST_MSG, "推进器模块未连接");
            }
        }
        return propellerWriteHandler;
    }

    @Override
    public ModuleReadHandler getReadHandler() {
        if (propellerReadHandler == null)
        {
            if (mainActivity != null)
            {
//                mainActivity.sendMsgToMainThread(MsgType.TOAST_MSG, "推进器模块未连接");
            }
        }
        return propellerReadHandler;
    }

    @Override
    public void releaseModuleResource() {
        if (this.asynchronousSocketChannel != null)
        {
            try
            {
                this.asynchronousSocketChannel.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        if (propellerReadHandler != null)
        {
            propellerReadHandler = null;
        }

        if (propellerWriteHandler != null)
        {
            propellerWriteHandler = null;
        }
    }
}
